﻿namespace MapleRock.P2PEmulator.Core
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Text;

  public static class Geometry
  {
    public static double EvaluateDistance(double[] p0, double[] p1)
    {
      Debug.Assert(p0.Length == p1.Length, "p0.Length == p1.Length");

      var length = 0.0;
      for (var i = 0; i < p0.Length; ++i)
      {
        length += Math.Pow(p0[i] - p1[i], 2.0);
      }

      return Math.Sqrt(length);
    }

    // evaluate an angle between the vectors (a0; a1) and (b0; b1)
    public static double EvaluateAngle(double[] a0, double[] a1, double[] b0, double[] b1)
    {
      Debug.Assert(a0.Length == a1.Length, "a0.Length == a1.Length");
      Debug.Assert(b0.Length == b1.Length, "b0.Length == b1.Length");
      Debug.Assert(a0.Length == b0.Length, "a0.Length == b0.Length");

      var vectorSize = a0.Length;

      var v0 = new double[vectorSize];
      var v1 = new double[vectorSize];

      for (var i = 0; i < vectorSize; ++i)
      {
        v0[i] = a1[i] - a0[i];
        v1[i] = b1[i] - b0[i];
      }

      var scalarProduct = 0.0;
      for (var i = 0; i < vectorSize; ++i)
      {
        scalarProduct += (v0[i] * v1[i]);
      }

      var length0 = Geometry.EvaluateDistance(a0, a1);
      var length1 = Geometry.EvaluateDistance(b0, b1);

      return Math.Acos(scalarProduct / (length0 * length1));
    }
  }
}
